package com.hl.controller;

import com.hl.domain.Consultproblem;
import com.hl.service.ConsultproblemService;
import com.hl.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/problem")
public class ConsultproblemController {
    @Autowired
   private ConsultproblemService consultproblemService;

    //发表问题，插入数据
    @PostMapping
    public R addProblem(@RequestBody Consultproblem consultproblem){
        int row = consultproblemService.addProblem(consultproblem);
        return row>0?R.success(row):R.fail(0);

    }

    //返回给前端问题列表
    @GetMapping
    public R getProblems(Integer pageNo,Integer pageSize){
        List<Consultproblem> consultproblems = consultproblemService.selectAllAnd(pageNo,pageSize);
        return consultproblems!=null?R.success(consultproblems):R.fail(null);
    }

    //给前端问题分页
    @GetMapping("/getProblemAndPage")
    public R getProblemAndPage(Integer pageNo,Integer pageSize){
        Map<String, Consultproblem> totalAndPage = consultproblemService.getTotalAndPage(pageNo, pageSize);
        return totalAndPage != null ? R.success(totalAndPage):R.fail(null);
    }

    //搜索模糊查询
    @GetMapping("/selectAllByLike")
    public R selectAllByLike(String problemtitle ,Integer pageNum, Integer pageSize, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")Date endTime){
        Map<String, Consultproblem> consultproblemMap = consultproblemService.selectAllByLike(problemtitle, pageNum, pageSize, startTime, endTime);
        return consultproblemMap != null ? R.success(consultproblemMap):R.success(null);
    }

    //根据id删除
    @DeleteMapping("/delete")
    public R deleteByPrimaryKey(Long id){
        int row = consultproblemService.deleteByPrimaryKey(id);
        return R.success(row);
    }
}
